<?php
namespace App\Admin\Controllers\Ext;

use App\Admin\Actions\Tools\BatchCate;
use Encore\Admin\Admin;

//分类扩展
class Cate
{

	/**
     * add a grid here.
     */
    public function before_grid($grid,array $app)
    {
		//$cate_id = request('cate_id',0);
		if(!empty($app['cate_id'])){
			$grid->model()->cateId($app['cate_id']);
		}
		$cates = get_cates($app);
		if( $app['sets']['has_cate_sets'] ){ //仅针对允许自定义设置的应用
			$grid->setCates($cates);
		}
		$cates_sel = [];
		foreach($cates as $v){
			$cates_sel[$v['id']] = $v['title'];
		}
		$grid->tools(function ($tools) use($app){
			$tools->append(new \App\Admin\Actions\Tools\CateSelect($app));
		});
	}

    public function grid($grid,array $app)
    {
		$grid->column('cate.title', '分类')->display(function() use ($app){
				return "<a href='".route('admin.apps.'.$app['name'].'.index')."?cate_id=".$this->cate['id']."'>".$this->cate['title']."</a>";
			});
		if(!empty($app['sets']['cate']['group'])){
			$grid->column('group', '分组');
		}
	}

    public function form($form,array $app)
    {
		$cates = get_cates($app);
		if(empty($cates)){
			admin_error('错误','没有任何可用分类，请先设置分类');
			back();				
		}
		$disoption = [];
		foreach($cates as $cate){
				if(empty($cate['publish'])){
					$disoption[] = $cate['id'];
				}	
		}
		$disoption = arr2str($disoption);
		
		$cate_id = request('cate_id',0);
		if( !empty($cate_id) && isset($cates[$cate_id])) {
			$cate = $cates[$cate_id];
		}else{
			$cate = [];
		}
		if(!empty($cate) && empty($cate['publish'])){
			admin_error('错误','当前分类不允许发表内容，请更改分类');
			back();	
		}
		if( !empty($app['sets']['cate']['group'])){
			$form->select('cate_id', '分类')->options(\App\Models\Cate::selectOptions(function ($query) use($app){
				return $query->where('app_name', $app['name']);
			}, '选择分类'))->load('group',route('admin.api.get_cate_groups'))->default($cate_id)->rules('required|integer');		
			$form->select('group','组')->options(function () use($cate,$cates){
				$groups = [];
				if(!empty($this->cate_id) && !empty($cates[$this->cate_id])){
					$cate = $cates[$this->cate_id];
				}
				if(!empty($cate) && !empty($cate['groups'])){
					$groups = tag2arr($cate['groups']);
				}
				return $groups;					
			});		
		}else{
			$form->select('cate_id', '分类')->options(\App\Models\Cate::selectOptions(function ($query) use($app){
				return $query->where('app_name', $app['name']);
			}, '选择分类'))->default($cate_id)->rules('required|integer');			
		}
$script = <<<EOT
(function () {
	var disoptions = [{$disoption}];
	$.each(disoptions, function(idx, v) {
		$("form select[name='cate_id'] option[value='"+v+"']").prop('disabled',true);
	});
	$("form select[name='cate_id']").select2();
})();
EOT;

        Admin::script($script);
	}


    public function before_show($show,array $app,array $cate=[])
	{
		if(  !empty($app['sets']['cate']['set']) ){
				if($rs && !empty($rs->cate_id)){
					$cate_id = $rs->cate_id;
				}else{
					$cate_id = request('cate_id',0);
				}

				if( !empty($cate_id) && $cate = get_xapp_cate($app['name'],$cate_id) ){
					$app['cate'] = $cate;
					if( $cate && !empty($cate['sets'])){		
						$app['sets'] = get_xapp_sets($app['name'],$cate_id);
					}
				}
		}	
	}

    public function show($show,array $app,array $cate=[])
    {
		$show->field('cate.title','分类')->as(function(){
				return $this->cate['title'];
			});
		$show->field('group','分组');
	}

}
